package com.bawei.bydayzg_two.viewModel

import androidx.lifecycle.viewModelScope
import com.bawei.base.bean.AddCartResult
import com.bawei.base.bean.DetailResult
import com.bawei.base.bean.GoodsPlResult
import com.bawei.base.bean.Res
import com.bawei.base.http.SingleLiveData
import com.bawei.base.mvvm.BaseViewModel
import kotlinx.coroutines.launch

class DetailViewModel:BaseViewModel() {

    val detailSuccess = SingleLiveData<Res<DetailResult>>()
    val detailFail = SingleLiveData<String>()
    //
    fun detail(id:Int,type:String){
        viewModelScope.launch {
            try {
                detailSuccess.postValue(apiService.detail(id, type))
            } catch (e: Exception) {
                detailFail.postValue(e.message.toString())
            }
        }
    }

    //添加购物车
    val addSuccess = SingleLiveData<Res<AddCartResult>>()
    val addFail = SingleLiveData<String>()
    //
    fun add(cartNum:Int,productAttrUnique:String,productId:Int){
        viewModelScope.launch {
            viewModelScope.launch {
                //
                try {
                    val map = mutableMapOf<String,Any>()
                    map["cartNum"] = cartNum
                    map["productAttrUnique"] = productAttrUnique
                    map["productId"] = productId
                    addSuccess.postValue(apiService.addCart(maoToBody(map)))
                } catch (e: Exception) {
                    addFail.postValue(e.message.toString())
                }
            }
        }
    }

    //
    val plSuccess = SingleLiveData<Res<GoodsPlResult>>()
    val plFail = SingleLiveData<String>()
    //
    fun pl(id:Int,limit:Int,page:Int,type:String){
        viewModelScope.launch {
            try {
                plSuccess.postValue(apiService.goodsPl(id, limit, page, type))
            } catch (e: Exception) {
                plFail.postValue(e.message.toString())
            }
        }
    }
}